﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using DbAdapter.sqldbAdapter;

namespace Water.Model
{
    public class BigValveOperatePlan
    {
        public string MRNO { get; set; }
        public string WorkNr { get; set; }
        public string WorkNo { get; set; }
        public DateTime FillDt { get; set; }
        public string Address { get; set; }
        public string ValveID { get; set; }
        public DateTime StopYdDt { get; set; }
        public DateTime StartYdDt { get; set; }
        public string BeforeOperZb { get; set; }
        public string PingGyx { get; set; }
        public string AfterOper { get; set; }
        public string WorkUnit { get; set; }
        public string TzhUser { get; set; }
        public string OperValveKsh { get; set; }
        public string OperImage { get; set; }
        public string OperEmp { get; set; }
        public DateTime CreateDt { get; set; }

        /// <summary>
        /// AddBigValveOperatePlan
        /// </summary>
        /// <param name="bvop"></param>
        /// <returns></returns>
        public int AddBigValveOperatePlan(BigValveOperatePlan bvop)
        {
            string sql = "Execute Proc_BigValveOperatePlan '" + bvop.MRNO + "','" + bvop.WorkNr + "','" + bvop.WorkNo + "','"
                + bvop.FillDt + "','" + bvop.Address + "','" + bvop.ValveID + "','" + bvop.StopYdDt + "','"
                + bvop.StartYdDt + "','" + bvop.BeforeOperZb + "','" + bvop.PingGyx + "','" + bvop.AfterOper + "','"
                + bvop.WorkUnit + "','" + bvop.TzhUser + "','" + bvop.OperValveKsh + "','"
                + bvop.OperImage + "','" + bvop.OperEmp + "'";
            sqldb db = new sqldb();
            //WriteSqlTotxt.writesql(sql);
            return db.ExecuteNonQuery(sqlconn.GetConnStr(), CommandType.Text, sql);
        }

        /// <summary>
        /// DeleteBigValveOperatePlan
        /// </summary>
        /// <param name="mrno"></param>
        /// <returns></returns>
        public int DeleteBigValveOperatePlan(string mrno,string vid)
        {
            string sql = "Execute Proc_DeleteBigValveOperatePlan '" + mrno + "','" + vid + "'";
            sqldb db = new sqldb();
            return db.ExecuteNonQuery(sqlconn.GetConnStr(), CommandType.Text, sql);
        }
    }

    public class BigValveOperatePlanQuery : BigValveOperatePlan
    {
        public virtual DateTime CloseDt { get; set; }
        public virtual DateTime StartDt { get; set; }
        public virtual int ZhuanCount { get; set; }
        public virtual string StartDirection { get; set; }

        public BigValveOperatePlanQuery GetBigValveOperatePlanQuery(string mrno)
        {
            string sql = " select a.MRNO,a.WorkNr,a.WorkNo,a.FillDt,a.Address,a.ValveID,a.StopYdDt,"
            + "a.StartYdDt,a.BeforeOperZb,a.PingGyx,"
            + "a.AfterOper,a.OperImage,b.CloseDt,b.StartDt,b.ZhuanCount,b.StartDirection,a.OperEmp,a.CreateDt "
            + "from Tbl_BigValveOperatePlan a inner join Tbt_BigValveOperate b on a.MRNO = b.MRNO where a.MRNO = '" + mrno + "'";
            sqldb db = new sqldb();
            //WriteSqlTotxt.writesql(sql);
            DataSet ds = db.ExecuteDataset(sqlconn.GetConnStr(), CommandType.Text, sql);
            BigValveOperatePlanQuery bvopq = new BigValveOperatePlanQuery();
            if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
            {
                DataRow dr = ds.Tables[0].Rows[0];
                bvopq.MRNO = dr["MRNO"].ToString();
                bvopq.WorkNr = dr["WorkNr"].ToString();
                bvopq.WorkNo = dr["WorkNo"].ToString();
                bvopq.FillDt = Convert.ToDateTime(dr["FillDt"]);
                bvopq.Address = dr["Address"].ToString();
                bvopq.ValveID = dr["ValveID"].ToString();
                bvopq.StopYdDt = Convert.ToDateTime(dr["StopYdDt"]);
                bvopq.CloseDt = Convert.ToDateTime(dr["CloseDt"]);
                bvopq.StartYdDt = Convert.ToDateTime(dr["StartYdDt"]);
                bvopq.StartDt = Convert.ToDateTime(dr["StartDt"]);
                bvopq.ZhuanCount = Convert.ToInt32(dr["ZhuanCount"]);
                bvopq.StartDirection = dr["StartDirection"].ToString();
                bvopq.BeforeOperZb = dr["BeforeOperZb"].ToString();
                bvopq.PingGyx = dr["PingGyx"].ToString();
                bvopq.AfterOper = dr["AfterOper"].ToString();
                bvopq.OperImage = dr["OperImage"].ToString();
                bvopq.OperEmp = dr["OperEmp"].ToString();
                bvopq.CreateDt = Convert.ToDateTime(dr["CreateDt"]);
            }
            return bvopq;
        }
    }
}
